import { reqGoodsInfo, reqAddOrUpdateCart} from "@/api"
import { getUUID } from '@/utils/uuid_token'
const state = {
  goodsInfo: {},
  uuid_token:getUUID()
}
const mutations = {
  GETGOODINFO(state,goodsInfo){
    state.goodsInfo = goodsInfo
  }
}
const actions = {
  // 获取商品信息
  async getGoodsInfo({commit},skuId){
    let res = await reqGoodsInfo(skuId)
    // console.log(res);
    if(res.data.code === 200){
      commit('GETGOODINFO',res.data.data)
    }
  },
  // 加入购物车||修改数量
  async aaddOrUpdateCart({commit}, {skuId,skuNum}){
    // 发请求，将参数带给服务器,存储成功，不返回数据，不需要三连环
    let res = await reqAddOrUpdateCart(skuId,skuNum)
    // console.log(res)
    if(res.data.code==200){
      return 'ok'
    }else{
      return Promise.reject(new Error('false'))
    }
  }
}
const getters = {
  // 路径导航
  categoryView(state){
    return state.goodsInfo.categoryView || {}
  },
  // 商品详情
  skuInfo(state){
    return state.goodsInfo.skuInfo || {}
  },
  // 售卖属性
  spuSaleAttrList(){
    return state.goodsInfo.spuSaleAttrList || {}
  }
}
export default{
  state,
  mutations,
  actions,
  getters
}